// LICENCE:
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the
// use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//    1. The origin of this software must not be misrepresented;
//       you must not claim that you wrote the original software.
//       If you use this software in a product, an acknowledgment
//       in the product documentation would be appreciated but is
//       not required.
//
//    2. Altered source versions must be plainly marked as such,
//       and must not be misrepresented as being the original software.
//
//    3. This notice may not be removed or altered from any source distribution.


unit desktop;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus, Aboutwindow, ExtCtrls, jpeg, ShellApi, Filectrl,
  Registry;

type
  TdGS = class(TForm)
	Timer1:TTimer;
	browseraddress:TEdit;
	Timer2:TTimer;
    PopupMenu1: TPopupMenu;
    StartMenu1: TMenuItem;
    N3: TMenuItem;
    Preferences1: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    background: TImage;
    ExitBetaTestingOnly2: TMenuItem;
    About1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure ExitBetatestingonly1Click(Sender: TObject);
    procedure Preferences1Click(Sender: TObject);
    procedure ExitBetaTestingOnly2Click(Sender: TObject);
	procedure browseraddressEnter(Sender: TObject);
    procedure browseraddressExit(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
	procedure showbrowser;
    procedure launchdefaultbrowser(Sender: TObject);
    procedure browseraddressMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure browseraddressClick(Sender: TObject);
    procedure browseraddressChange(Sender: TObject);
    function GetCurrentUserName : string;
    procedure Button1Click(Sender: TObject);
    procedure StartitemClick(Sender:Tobject);
    procedure About1Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  dGS: TdGS;
	webbrowservisible:boolean=false;
  browpic:TGraphic;
  jpeg:tJpegImage;
  bmp:tbitmap;
  browsepic:timage;
  gopic:timage;
  startmenupath:string[255];
  fileslist:TFileListbox;
    dirlist:TdirectoryListBox;
    startmenudir:string;
    ii:integer; {needs to be global.. sorry for the naming}
implementation

uses pref, startmenu;

{$R *.dfm}

procedure TdGS.FormCreate(Sender: TObject);
var windir:string;
    ilength:integer;
    i,dir:integer;
    username:string;
    startitem:Tmenuitem;

    rightdir:integer;
    reg:tregistry;
    directory:string;
begin
  {Desktop Background}
{
this doesn;t seem to work.
 with background do
    Top := 0;
    Left := 0;
    Width := Screen.Width;
    Height := screen.Height;}

    background.Width := screen.Width;
    background.Height := screen.height;

{get windir and calculate start menu items from there. also username needed}
// Initialize Variable
iLength := 255;
setLength(WinDir, iLength);
iLength := GetWindowsDirectory(PChar(WinDir), iLength);
setLength(WinDir, iLength);
username:=GetCurrentUserName;
startmenupath:=windir[1]+':\Documents and settings\'+username;
startmenupath:=startmenupath+'\Start Menu\';
{ ^^ and there you have teh path! next on to filling the menu}
{create a runtime filelistbox to get directory struct}
 fileslist:=tfilelistbox.create(self);
 fileslist.parent:=self;
 fileslist.width:=screen.width;
 fileslist.visible:=false;
 fileslist.Drive:=windir[1];
 fileslist.Directory:=startmenupath;  {wont add it straight after so i fixed it.. weird bug..}


 {now we just need a dir list aswell}
  dirlist:=tdirectorylistbox.create(self);
  with dirlist do
  begin
  parent:=self;
  visible:=false;
  drive:=windir[1];
  directory:=fileslist.directory;
  end;
   reg:=TRegistry.Create;
   try
     reg.RootKey := HKEY_CURRENT_USER;
     if reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\', True) then
     begin
         fileslist.directory:=reg.ReadString('Programs');
         dirlist.Directory:=reg.ReadString('Programs');
         startmenudir:=reg.ReadString('Programs');
        end;
       reg.CloseKey;
     finally
     reg.Free;
   end;

  {oki so now we can surf the dir and the files.. now on to adding it}
//add items to right mouse click
   //get first level of start menu and assign an onclick handler to take care of the rest
    for i:=0 to fileslist.items.Count-1 do begin
    startitem := tmenuitem.create(self);
    startitem.Caption:=fileslist.Items.Strings[i];
    startitem.hint:=fileslist.items.strings[i];
    startitem.Tag:=i;
    startitem.OnClick:=StartItemClick;

    popupmenu1.Items[0].add(startitem);
    end;
    for ii:=4 to dirlist.Items.Count-1 do begin
    startitem := tmenuitem.create(self);
    startitem.caption:=dirlist.items.strings[ii];
    startitem.Hint:=dirlist.items.strings[ii];
    startitem.Tag:=ii;
    startitem.OnClick := StartItemClick;

    popupmenu1.Items[0].add(startitem);
    end;
    dir:=dirlist.items.count-1;



    {egmos crappy start code. sorry for the mess. Create browser on teh fly}
browsepic:=timage.Create(self); {Quick lesson in creating runtime images}
browsepic.Parent:=self;         {All that needs to be done is having a variable declared as timage}
browsepic.Left:=0;               {then doing variable:=timage.create(self); and assigning the properties}
browsepic.Top:=0;                {like position visibility stretching and stuff and of course loading the pic}
browsepic.Width:=screen.width;    {if you loop it you can have many images with the same variable}
browsepic.Height:=40;             {for that i suggest finding a property where you can store some info to recognize}
browsepic.Stretch:=true;          {which one of the images it is. Also setting an event handler is simple}
browsepic.visible:=false;          {just do yourimage.OnClick:=yourimageclick; and make the necessary procedure}
browsepic.Picture.LoadFromFile(extractfilepath(application.exename)+'\images\brow.jpg');
//go button

gopic:=timage.Create(self);
gopic.Parent:=self;
gopic.Left:=(screen.width div 2 + 100);
gopic.Top:=5;
gopic.Width:=30;
gopic.Height:=30;
gopic.Stretch:=true;
gopic.visible:=false;
gopic.Picture.LoadFromFile(extractfilepath(application.exename)+'\images\go.jpg');
gopic.OnClick:=launchdefaultbrowser;
browseraddress.text:='http://www.degekkenclub.tk';

end;

procedure TdGS.ExitBetatestingonly1Click(Sender: TObject);
begin
  dGS.Close;
end;

procedure TdGS.Preferences1Click(Sender: TObject);
begin
  prefwindow.ShowModal;
end;

procedure TdGS.ExitBetaTestingOnly2Click(Sender: TObject);
begin
  dGS.Close;
end;

//egmos crappy routines
procedure TdGS.Timer1Timer(Sender: TObject);
var
p : TPoint;
begin
GetCursorPos(p);
if p.Y < 10 then begin
  if webbrowservisible=false then begin webbrowservisible:=true; showbrowser; end;
  end;
if p.y>40 then timer2.enabled:=true;
end;

procedure TdGS.showbrowser;
var picarea:trect;
begin
picarea:=Rect(Point(1, 1), Point(screen.width, 39));
//make the browser come out


with browseraddress do begin
  top:=10;
  left:=(screen.Width div 2 - 200);
  width:=300;

  end;
browseraddress.Visible:=true;
browsepic.Visible:=true;
gopic.visible:=true;
browseraddress.BringToFront;
timer2.enabled:=true;
end;
procedure TdGS.Timer2Timer(Sender: TObject);
var p:tpoint;
begin
GetCursorPos(p);
if (p.y>40) then begin
  browseraddress.visible:=false;

  browsepic.Visible:=false;
  gopic.visible:=false;
  webbrowservisible:=false;
  end;
end;



procedure TdGS.browseraddressExit(Sender: TObject);
begin
timer2.enabled:=true;
end;

procedure TdGS.browseraddressEnter(Sender: TObject);
begin
timer2.enabled:=false;
end;

procedure TdGS.launchdefaultbrowser(Sender:Tobject);
begin
//check for your def browser and launch it
ShellExecute(Handle, 'open', PChar(browseraddress.text), nil, nil, SW_SHOWNORMAL);
  inherited;

end;


procedure TdGS.browseraddressMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
begin
timer2.enabled:=false;
end;

procedure TdGS.browseraddressClick(Sender: TObject);
begin
timer2.enabled:=false;
end;

procedure TdGS.browseraddressChange(Sender: TObject);
begin
timer2.enabled:=false;
end;

function TdGS.GetCurrentUserName : string;
const
  cnMaxUserNameLen = 254;
var
  sUserName     : string;
  dwUserNameLen : DWord;
begin
  dwUserNameLen := cnMaxUserNameLen-1;
  SetLength( sUserName, cnMaxUserNameLen );
  GetUserName(
    PChar( sUserName ),
    dwUserNameLen );
  SetLength( sUserName, dwUserNameLen );
  Result := sUserName;
end;

procedure TdGS.Button1Click(Sender: TObject);
begin
  startpanel.Show;


end;

procedure TdGS.StartitemClick(Sender:Tobject);
var menu:Tpopupmenu;
    item:Tmenuitem;
    i:integer;
    p:Tpoint;
    ext:string; {file extension}
begin
//in case something is pressed on the start menu XD
menu:=tpopupmenu.Create(self);
with Sender as Tmenuitem do begin
browseraddress.text:=fileslist.directory+'\'+hint;
if fileexists(fileslist.Directory+'\'+hint) then
  begin
  //if its a file you just clicked then execute it with shellexecute.. thats sooo nifty =)
  ShellExecute(Handle, nil, PChar(fileslist.Directory+'\'+hint), nil, nil, SW_SHOWNORMAL);
  inherited;
  end
  else
  begin
dirlist.Directory:=dirlist.directory+'\'+hint+'\';
fileslist.directory:=fileslist.Directory+'\'+hint+'\';

for i:=0 to fileslist.items.Count-1 do begin
    item := tmenuitem.create(self);
    item.Caption:=fileslist.Items.Strings[i];
    item.hint:=fileslist.Items.Strings[i];
    item.OnClick:=StartItemClick;
    menu.Items.add(item);
    end;
    for ii:=ii to dirlist.Items.Count-1 do begin
    item := tmenuitem.create(self);

    item.caption:=dirlist.items.strings[ii];
    item.Hint:=dirlist.items.strings[ii];
    item.OnClick := StartItemClick;
    menu.Items.add(item);

    end;

  fileslist.directory:=startmenudir;  {<-- those two lines need to be placed somewhere to prevent the bug }
  dirlist.Directory:=startmenudir;    {<--}    
  
    end;
GetCursorPos(p);

menu.Popup(p.x,p.y);


end;
       end;


procedure TdGS.About1Click(Sender: TObject);
begin
AboutForm.Visible:=true;
end;

end.
